package collision;

import data.NavmeshRegion;
import instruments.FullFilePathFinderInstrument;
import instruments.GlobalPathInstrument;
import instruments.LocalFinderInstrument;
import instruments.NewPolyCreateInstrument;
import instruments.NewPolygonInstrument;
import instruments.Recalculator;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author sad
 */
public class MainWindow extends javax.swing.JFrame {
    private static MainWindow instance;
    private List<NavmeshRegion> extAreas = new ArrayList<>();

    public List<NavmeshRegion> getExtAreas() {
        return extAreas;
    }

    public void setExtAreas(List<NavmeshRegion> extAreas) {
        this.extAreas = extAreas;
    }

    public MainWindow() {
        initComponents();
        instance = this;
        getDrawField().setInstrument(new NewPolygonInstrument());
        setLocationRelativeTo(null);
    }

    public static MainWindow getInstance() {
        return instance;
    }

    public JDrawField getDrawField() {
        return drawField;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        drawField = new collision.JDrawField();
        newPolyButton = new javax.swing.JButton();
        gridSizeTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        calcPolyButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        addPolyButton = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        pointPolygonButton = new javax.swing.JButton();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        drawField.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));

        javax.swing.GroupLayout drawFieldLayout = new javax.swing.GroupLayout(drawField);
        drawField.setLayout(drawFieldLayout);
        drawFieldLayout.setHorizontalGroup(
            drawFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 377, Short.MAX_VALUE)
        );
        drawFieldLayout.setVerticalGroup(
            drawFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 508, Short.MAX_VALUE)
        );

        newPolyButton.setText("NewPolygon");
        newPolyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newPolyButtonActionPerformed(evt);
            }
        });

        gridSizeTextField.setText("20");
        gridSizeTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gridSizeTextFieldActionPerformed(evt);
            }
        });

        jLabel1.setLabelFor(gridSizeTextField);
        jLabel1.setText("Grid size");

        calcPolyButton.setText("calculatePolygons");
        calcPolyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calcPolyButtonActionPerformed(evt);
            }
        });

        jButton2.setText("Global Finder");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Local Finder");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        addPolyButton.setText("add poly");
        addPolyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPolyButtonActionPerformed(evt);
            }
        });

        jButton4.setText("FullPathFinder");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        pointPolygonButton.setText("addPointPolygon");
        pointPolygonButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pointPolygonButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(drawField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(newPolyButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(gridSizeTextField)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pointPolygonButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(calcPolyButton, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                    .addComponent(addPolyButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(52, 52, 52))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(newPolyButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(gridSizeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pointPolygonButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addPolyButton)
                .addGap(87, 87, 87)
                .addComponent(calcPolyButton)
                .addContainerGap(162, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(drawField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void newPolyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newPolyButtonActionPerformed
        getDrawField().setInstrument(new NewPolygonInstrument());
    }//GEN-LAST:event_newPolyButtonActionPerformed

    private void gridSizeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gridSizeTextFieldActionPerformed
        try {
            double step = Double.parseDouble(gridSizeTextField.getText());
            //drawField.setGRID_SIZE(step);
            getDrawField().redraw();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "It is not a double value");
        }
    }//GEN-LAST:event_gridSizeTextFieldActionPerformed

    private void calcPolyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calcPolyButtonActionPerformed
        Recalculator recalculator = new Recalculator(getExtAreas());
        recalculator.recalculate();
        getDrawField().redraw();
    }//GEN-LAST:event_calcPolyButtonActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        getDrawField().setInstrument(new GlobalPathInstrument());
        getDrawField().redraw();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        getDrawField().setInstrument(new LocalFinderInstrument());
        getDrawField().redraw();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void addPolyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPolyButtonActionPerformed
    }//GEN-LAST:event_addPolyButtonActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        getDrawField().setInstrument(new FullFilePathFinderInstrument());
        getDrawField().redraw();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void pointPolygonButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pointPolygonButtonActionPerformed
        getDrawField().setInstrument(new NewPolyCreateInstrument());
        getDrawField().redraw();
    }//GEN-LAST:event_pointPolygonButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addPolyButton;
    private javax.swing.JButton calcPolyButton;
    private collision.JDrawField drawField;
    private javax.swing.JTextField gridSizeTextField;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton newPolyButton;
    private javax.swing.JButton pointPolygonButton;
    // End of variables declaration//GEN-END:variables
}
